﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;

namespace ImagePoof
{
    class PoofLib
    {
        public static bool CheckInputFile(string f)
        {
            string ext = System.IO.Path.GetExtension(f);
            switch (ext.ToLower())
            {
                case ".jpg":
                    return true;
                case ".jpeg":
                    return true;
                case ".png":
                    return true;
                case ".ico":
                    return true;
                case ".bmp":
                    return true;
                case ".tiff":
                    return true;
                case ".gif":
                    return true;
                default:
                    return false;
            }
        }

        public static string[] RemoveNonImages(string[] inputArray)
        {
            string[] temp = new string[inputArray.Length];
            int i = 0;
            for (int x = 0; x < inputArray.Length; x++)
            {
                if (PoofLib.CheckInputFile(inputArray[x]))
                {
                    temp[i] = inputArray[x];
                    i++;
                }
            }
            if (i == 0)
                return new string[] { };
            else
            {
                string[] temp2 = new string[i];
                for (int x = 0; x < i; x++)
                    temp2[x] = temp[x];
                return temp2;
            }
        }

        public static Bitmap MakeGrayscale(Bitmap original)
        {
            //create a blank bitmap the same size as original
            Bitmap newBitmap = new Bitmap(original.Width, original.Height);

            //get a graphics object from the new image
            Graphics g = Graphics.FromImage(newBitmap);

            //create the grayscale ColorMatrix
            ColorMatrix colorMatrix = new ColorMatrix(
               new float[][] 
                  {
                     new float[] {.3f, .3f, .3f, 0, 0},
                     new float[] {.59f, .59f, .59f, 0, 0},
                     new float[] {.11f, .11f, .11f, 0, 0},
                     new float[] {0, 0, 0, 1, 0},
                     new float[] {0, 0, 0, 0, 1}
                  });

            //create some image attributes
            ImageAttributes attributes = new ImageAttributes();

            //set the color matrix attribute
            attributes.SetColorMatrix(colorMatrix);

            //draw the original image on the new image
            //using the grayscale color matrix
            g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
               0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);

            //dispose the Graphics object
            g.Dispose();
            return newBitmap;
        }

        public static Bitmap ResizeImage(Image image, int width, int height)
        {
            //a holder for the result
            Bitmap result = new Bitmap(width, height);

            //use a graphics object to draw the resized image into the bitmap
            using (Graphics graphics = Graphics.FromImage(result))
            {
                //set the resize quality modes to high quality
                graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                //draw the image into the target bitmap
                graphics.DrawImage(image, 0, 0, result.Width, result.Height);
            }

            //return the resulting bitmap
            return result;
        }
    }
}
